<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
  >
    <el-transfer
      v-model="formModel"
      filterable
      :titles="['可选授权策略名称A', '已选授权策略名称']"
      :button-texts="['移除策略', '授权策略']"
      :format="{
        noChecked: '${total}',
        hasChecked: '${checked}/${total}'
      }"
      :data="data"
      @change="handleChange"
    >
      <div
        slot-scope="{ option }"
        class="item"
      >
        <div>
          <h2>{{ option.label }}</h2>
          <p>{{ option.description }}</p>
        </div>
        <span>{{ option.type }}</span>
      </div>
    </el-transfer>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="submitForm"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'AssignDialogForm',
  props: {
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    },
    // 数据接口
    apiService: {
      type: Function,
      default: () => {
        return
      }
    }
  },
  data() {
    return {
      formModel: [],
      requestId: [],
      dialogVisible: false,
      dialogTitle: '',
      id: 0,
      data: [],
      assign: [],
      revoke: []
    }
  },
  computed: {},
  watch: {},
  mounted() {
    this.dialogVisible = this.visible
    this.dialogTitle = this.title
  },
  methods: {
    // 提交表单数据
    submitForm() {
      const data = {
        items: this.formModel
      }
      this.$http.rbac.assign(this.id, data).then(response => {
        const { data } = response
        console.log(data)
        this.$emit('submit', this.formModel)
        this.dialogVisible = false
      })
    },
    handleChange(value, direction, movedKeys) {
      this.formModel = value
      if (direction === 'left') {
        this.revoke = movedKeys
      }
      if (direction === 'right') {
        this.assign = movedKeys
      }
    }
  }
}
</script>

<style lang="scss" scope>
.transfer-footer {
  margin-left: 20px;
  padding: 6px 5px;
}

.el-transfer {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.el-transfer-panel {
  flex: 1;
  &__body {
    height: 400px;
  }
  &__list.is-filterable {
    height: 100%;
  }
  &__item {
    .el-checkbox__input {
      display: none;
    }
  }
}

.el-transfer__buttons {
  display: flex;
  flex-direction: column;
  .el-button + .el-button {
    margin: 0;
  }
}

.el-transfer-panel__item.el-checkbox {
  margin: 0;
  padding: 10px;
  height: auto;
  line-height: initial;
  width: 100%;

  &::after {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 1px;
    border-bottom: 1px solid #f3f4f5;
    background: #f3f4f5;
    width: 100%;
    content: '';
  }
  .el-checkbox__label {
    line-height: initial;
    padding: 0;
    .item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      h2 {
        font-size: 14px;
        margin: 0;
      }
      p {
        font-size: 12px;
        margin: 0;
        font-weight: 300;
        overflow: hidden;
        color: #ccc;
      }
      span {
        align-self: flex-start;
        font-size: 12px;
        font-weight: 300;
      }
    }
  }
  &.is-checked {
    background: #1890ff;
    .el-checkbox__label {
      color: #fff;
      p {
        color: rgba(255, 255, 255, 0.5);
      }
    }
  }
}
</style>
